// 📦 工具与依赖
const _ = require("lodash");
const esm = require("esm");
const requireModule = esm(module);

// 📦 模块加载
const { readAllValues } = requireModule(
  "../../../awakened-potential/helpers/readAllValues.js"
);
const checkAccuracy = requireModule("../../utils/checkAccuracy.js");

// ✅ 策略模块导入
const select32ByS1 = requireModule("../v1/select32NumbersByS1.js");
const select32ByS2 = requireModule("../v2/select32NumbersByS2.js");
const select32ByS3 = requireModule("../v3/select32NumbersByS3.js");

// ✅ 基础配置
const allValues = readAllValues("m");
const useWindow = true;
const windowSize = 50;
const predictResults = [];

// ✅ 开始预测循环
allValues.forEach((item, index) => {
  const temp = useWindow
    ? allValues.slice(Math.max(0, index - windowSize), index)
    : allValues.slice(0, index);

  if (!temp.length) return;

  // 🧠 获取三策略结果
  const s1 = select32ByS1(temp);
  const s2 = select32ByS2(temp);
  const s3 = select32ByS3(temp);

  // 🗳️ 所有号码投票计数
  const voteMap = {};
  [s1, s2, s3].forEach((arr) => {
    arr.forEach((num) => {
      voteMap[num] = (voteMap[num] || 0) + 1;
    });
  });

  // 🎯 选出票数最多的前 32 个号码
  const final = Object.entries(voteMap)
    .sort((a, b) => b[1] - a[1]) // 按票数降序
    .map(([num]) => num)
    .slice(0, 32);

  // ✅ 判断是否命中
  const isHit = checkAccuracy(final, item.number);
  predictResults.push(isHit);
});

// ✅ 统计分析
const total = predictResults.length;
const correct = predictResults.filter((x) => x === 1).length;
const accuracy = total > 0 ? ((correct / total) * 100).toFixed(2) : 0;

let maxWrongStreak = 0;
let currentStreak = 0;
predictResults.forEach((res) => {
  if (res === 0) {
    currentStreak++;
    maxWrongStreak = Math.max(maxWrongStreak, currentStreak);
  } else {
    currentStreak = 0;
  }
});

// ✅ 输出结果
console.log(`🤖 使用 S5 策略（S1+S2+S3 投票）`);
console.log(`预测总数: ${total}`);
console.log(`命中次数: ${correct}`);
console.log(`准确率: ${accuracy}%`);
console.log(`最大连续错误次数: ${maxWrongStreak}`);
